<template>
  <div class="login_conn">
    <img style="width: 100%; hight: 100%" src="../assets/images/field.jpg">
    <div class="login_box">
      <div style="width: 60%;height: 100%">
        <img src="../assets/images/65544.jpg" width="100%" height="100%" >
      </div>
      <div style=" background-color: #ffffff;width: 40%;height: 100%;text-align: center">
        <h1 style="margin-top: 50px;color: #409eff">欢迎登录惠农鲜品</h1>
        <el-form class="form_login" :rules="loginRules" :model="user" ref="loginForm">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user-solid" v-model="user.username" placeholder="请输入用户名"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-unlock" v-model="user.password" show-password placeholder="请输入密码">></el-input>
          </el-form-item>
		  <el-row :gutter="10">
		  <el-col :span="10">
			  <div>
				  <el-link type="default" href="/register" >还没有账号？立刻注册</el-link>
			  </div>
			</el-col>
			<el-col :span="8">
				<div class="flex-grow"/>
			</el-col>
			<el-col :span="6">
				<div>
					<el-link type="primary">忘记密码</el-link>
				</div>
			</el-col>
			</el-row>
          <el-form-item class="btns">
            <el-button type="primary" @click="login1" style="width: 100%;">登录</el-button>
            <el-button type="info" @click="LoginReset" style="width: 100%;margin-left: 0px;margin-top: 5px" >重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script  lang="ts">
// import { initRouter } from '../router/index'
import axios from 'axios';
import {loginUser} from "@/api/user"
import {jwtDecode} from "jwt-decode";
import { useAuthStore } from "@/stores";
// import ref from 'vue'
export default {
  data() {
    return {
      user: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 5 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //   重置方面
	LoginReset() {
	  this.user = {}
	  this.$refs.loginForm.resetFields()
	},
	// login(){
	// 	  this.$refs.loginForm.validate(async (valid) => {
	// 	                if (valid) {
	// 						const userId= this.user.username;
	// 						const password = this.user.password
	// 						console.log("pass",password)
	// 						console.log("id",userId)
	// 						const url = "http://localhost:9090/store/user/login/"+userId+"/"+password
	// 	                    axios.post(url,this.loginForm).then(res=>{
	// 							console.log(res.data)
	// 		                    if(res.data=='登录成功'){
	// 		                        localStorage.setItem("user",JSON.stringify(res.data));//存储用户信息到浏览器
	// 		                        this.$router.push("/");
	// 		                        this.$message.success("登录成功");
	// 		                    }else{
	// 		                        this.$message.error(res.data);
	// 		                    }
	// 	                    })
	// 	                }  
	// 	            })
	// },
	login1(){
		this.$refs.loginForm.validate(async (valid) => {
			if (valid) {
				const userId= this.user.username;
				const password = this.user.password
				// console.log("pass",password)
				// console.log("id",userId)
				// console.log("data===",this.user)
				loginUser(this.user).then((res) => {
				localStorage.setItem('userName',this.user.username);
				// console.log('token',res.data.authorization)
				localStorage.setItem('token',res.data.authorization)
				localStorage.setItem('res',JSON.stringify(res));//存储用户信息到浏览器
				console.log("======res=====",res)
				
				if(res.msg=='成功'){
				const store = useAuthStore();
				const tokenStr = localStorage.getItem('token')
				const decode = jwtDecode(tokenStr);
				// console.log("decodeing the token:=============",decode);//decode是一个对象
				//存储token解析的内容 以及修改登录的状态
				store.setAuth(!!decode) //由于decode是对象，所以对他取反再取反，双非就变成了布尔类型
				store.setUser(decode)
				// console.log('token=====',tokenStr)
				    this.$router.push("/home");
				    this.$message.success(res.msg);
				}else{
				    this.$message.error(res.msg);
				}
			})
		}
	})
},
}}
</script>

<style lang="less" scoped>
.login_conn {
  background-image: url("../assets/ba.jpg");
  background-color: #1D1C2E;
  height: 100%;
  background-size: 100% 100%;
  box-sizing: border-box;
  overflow: hidden;
}

.login_box {
  width: 60%;
  height: 50%;
  position: absolute;
  left: 20%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  margin-left: 30%;
  justify-content: flex-end;
  .img_box {
    //box-shadow: 0 0 10px #dddddd;
    height: 130px;
    width: 130px;
    padding: 10px;
    position: absolute;
    left: 50%;
    background-color: #ffffff;
  }
}

.btns {
  //display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.form_login {
  position: absolute;
  bottom: 40px;
  width: 40%;
  padding: 0 20px;
  box-sizing: border-box;

}
.el-row {
  margin-bottom: 20px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
</style>
